import { constObjs } from "@/const/const";
import { LandingAgency, OutlayStream, Partners } from "@/interface/entity";
import store from "@/store";
import { ElMessage } from "element-plus";
import { myDateFormat } from "./general";

export const processOutlayStreamBeforeToDb = (
  outlayStream: OutlayStream,
  method = ""
) => {
  // console.log("todb", outlayStream);
  const stream = JSON.parse(JSON.stringify(outlayStream)) as OutlayStream;
  const project = store.state.selectedProject;
  if (method == "add") {
    stream.projectGuid = project.projectGuid;
    stream.categories = project.categories;
    stream.projectName = project.projectName;
  }
  // stringfy
  stream.createdDate = myDateFormat(stream.createdDate);
  // 支出合计要初始化为0重新计算
  stream.totalPay = 0;

  // calculate totalpay
  Object.keys(stream).forEach(key => {
    // if now key is  “payxx”
    if (key.length == 4) {
      // get value of a key of a object in typescript! execellent!
      const nowPay = parseFloat(stream[key as keyof OutlayStream] as string);
      if (nowPay != null && !isNaN(nowPay)) {
        (stream.totalPay as number) += nowPay;
      } else {
        ElMessage.error(`${nowPay}:转换错误`);
        return;
      }
    }
  });
  // todo 把支出流水的落地机构和技术方的分成也计算到支出里面
  // todo client
  (stream.clients as LandingAgency[]).forEach(
    client => (stream.totalPay += client.shareAmount)
  );
  (stream.landingAgencies as LandingAgency[]).forEach(
    agency => (stream.totalPay += agency.shareAmount)
  );
  (stream.partners as Partners[]).forEach(
    partner => (stream.totalPay += partner.shareAmount)
  );
  // stringfy
  // todo client
  stream.clients = JSON.stringify(stream.clients);
  stream.landingAgencies = JSON.stringify(stream.landingAgencies);
  stream.partners = JSON.stringify(stream.partners);
  // console.log("todb", stream);
  return stream;
};

// todo process outlaystreams before show
export const generateOutlayStreamLists = (
  outlayStreams: OutlayStream[]
): OutlayStream[] => {
  const streams = JSON.parse(JSON.stringify(outlayStreams)) as OutlayStream[];
  // console.log("1", streams);
  let maxLandingAgenciesIndex = 0,
    maxPartnersIndex = 0,
    maxLandingAgenciesNum = 0,
    maxPartnersNum = 0,
    maxClientsIndex = 0,
    maxClientsNum = 0;

  streams.forEach((stream, index) => {
    // console.log(stream, index);
    if (stream.landingAgencies == undefined || stream.partners == undefined) {
      return outlayStreams;
    }
    // todo clients
    const clients = JSON.parse(stream.clients as string) as LandingAgency[];
    const landingAgencies = JSON.parse(
      stream.landingAgencies as string
    ) as LandingAgency[];
    const partners = JSON.parse(stream.partners as string) as Partners[];

    // todo clients
    stream.clients = clients;
    stream.landingAgencies = landingAgencies;
    stream.partners = partners;

    // todo client
    if (clients.length > maxClientsNum) {
      maxClientsNum = clients.length;
      maxClientsIndex = index;
    }
    if (landingAgencies.length > maxLandingAgenciesNum) {
      maxLandingAgenciesNum = landingAgencies.length;
      maxLandingAgenciesIndex = index;
    }
    if (partners.length > maxPartnersNum) {
      maxPartnersNum = partners.length;
      maxPartnersIndex = index;
    }
  });
  // console.log("2", streams);

  constObjs.outlayStreamTablePattern.forEach(pattern => {
    if (pattern.prop == "clients") {
      pattern.maxLengthIndex = maxClientsIndex;
      pattern.maxSubItemNum = maxClientsNum;
    }
    if (pattern.prop == "landingAgencies") {
      pattern.maxLengthIndex = maxLandingAgenciesIndex;
      pattern.maxSubItemNum = maxLandingAgenciesNum;
    }
    if (pattern.prop == "partners") {
      pattern.maxLengthIndex = maxPartnersIndex;
      pattern.maxSubItemNum = maxPartnersNum;
    }
  });
  // console.log("3", streams, constObjs.outlayStreamTablePattern);

  return streams;
};
